Public Class formMain
    Dim SC As WebCrawler
    Dim ntfy As New ProgNotify

    Private Sub formMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SC = New SclCrawler(ntfy)
        SC.Import()
        CreateFilterList()

        Dim Crawled As Boolean = False
        Dim args() As String
        args = System.Environment.GetCommandLineArgs
        Dim i As Integer
        If args.Length > 1 Then
            For i = 1 To args.Length - 1
                Select Case Mid(args(i), 1, 1)
                    Case "/"

                    Case "-"

                    Case Else
                        My.Application.DoEvents()
                        textDataUrl.Text = args(i)
                        My.Application.DoEvents()
                        DoCrawl()
                        Crawled = True
                        My.Application.DoEvents()
                End Select
            Next
        End If

        If Crawled Then
            End
        End If
    End Sub

    Private Sub btnCrawlAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrawlAll.Click
        Dim url() As String
        ReDim url(textDataUrl.Items.Count - 1)

        Dim i As Integer
        For i = 0 To textDataUrl.Items.Count - 1
            url(i) = textDataUrl.Items(i)
        Next

        For i = 0 To url.Length - 1
            textDataUrl.Text = url(i)
            My.Application.DoEvents()

            Crawl(url(i))
            My.Application.DoEvents()
        Next

        SC.Export()
        My.Application.DoEvents()

        CreateFilterList()
    End Sub

    Private Sub btnCrawl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrawl.Click
        DoCrawl()
    End Sub

    Private Sub DoCrawl()
        Crawl(textDataUrl.Text)

        SC.Export()
        My.Application.DoEvents()

        CreateFilterList()
    End Sub

    Private Sub Crawl(ByVal Url As String)
        Dim stype As RaidData.StatType = RaidData.StatType.StasisCL

        If radioWWS.Checked Then
            stype = RaidData.StatType.WWS
        ElseIf radioWMO.Checked Then
            stype = RaidData.StatType.WMO
            SC = New WmoCrawler(ntfy)
            SC.Import()
        ElseIf radioStasisCL.Checked Then
            stype = RaidData.StatType.StasisCL
            SC = New SclCrawler(ntfy)
            SC.Import()
        End If

        Dim RD As New RaidData
        RD.BaseUrl = Url
        RD.Region = textRegion.Text
        RD.Realm = textRealm.Text
        RD.RaidName = textRaidName.Text
        RD.Type = stype

        SC.Crawl(RD, chkFullUpdate.Checked)

        My.Application.DoEvents()
    End Sub

    Private Sub textDataUrl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textDataUrl.SelectedIndexChanged
        Dim r As Long

        If SC.Raids Is Nothing Then Exit Sub

        For r = 0 To SC.Raids.Length - 1
            If LCase(SC.Raids(r).BaseUrl) = LCase(textDataUrl.Text) Then
                textRegion.Text = SC.Raids(r).Region
                textRealm.Text = SC.Raids(r).Realm
                textRaidName.Text = SC.Raids(r).RaidName

                Select Case SC.Raids(r).Type
                    Case RaidData.StatType.StasisCL
                        radioStasisCL.Checked = True
                    Case RaidData.StatType.WMO
                        radioWMO.Checked = True
                    Case RaidData.StatType.WWS
                        radioWWS.Checked = True
                    Case Else
                        radioStasisCL.Checked = True
                End Select
                Exit For
            End If
        Next
    End Sub

    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Select Case SaveFileDialog1.ShowDialog()
            Case Windows.Forms.DialogResult.Cancel
                Exit Sub
            Case Windows.Forms.DialogResult.Abort
                Exit Sub
            Case Windows.Forms.DialogResult.No
                Exit Sub
            Case Windows.Forms.DialogResult.None
                Exit Sub
        End Select


        Dim RDAcc As New StatsDataAccess
        Dim Ftr As New StatsDataAccess.Filters

        Ftr.BaseUrl = textDataUrl2.Text

        Ftr.AreaName = textArea.Text
        Ftr.BossName = textBoss.Text
        Ftr.CharaClass = textClass.Text
        Ftr.CharaName = textChara.Text
        Ftr.DamageOut = chkDmgOut.Checked
        Ftr.DamageIn = chkDmgIn.Checked
        Ftr.Heal = chkHeal.Checked

        Dim FtrRaidData(0) As RaidData
        RDAcc.CopyRaidData(SC.Raids, FtrRaidData, Ftr)

        Dim ExportRaidData() As RaidData = FtrRaidData

        If chkCreateCharaReport.Checked Then
            RDAcc.CalcurateCharaReport(FtrRaidData, ExportRaidData)
        End If

        RDAcc.Export(ExportRaidData, SaveFileDialog1.FileName)
    End Sub

    Private Sub CreateFilterList()
        Dim r As Long
        Dim b As Long
        Dim c As Long

        textDataUrl.Items.Clear()

        textDataUrl2.Items.Clear()
        textArea.Items.Clear()
        textBoss.Items.Clear()
        textClass.Items.Clear()
        textChara.Items.Clear()

        textDataUrl2.Items.Add("*")
        textArea.Items.Add("*")
        textBoss.Items.Add("*")
        textClass.Items.Add("*")
        textChara.Items.Add("*")

        If SC.Raids IsNot Nothing Then
            For r = 0 To SC.Raids.Length - 1
                AddList(textDataUrl, SC.Raids(r).BaseUrl)
                AddList(textDataUrl2, SC.Raids(r).BaseUrl)
                If SC.Raids(r).Bosses IsNot Nothing Then
                    For b = 0 To SC.Raids(r).Bosses.Length - 1
                        AddList(textArea, SC.Raids(r).Bosses(b).Area)
                        AddList(textBoss, SC.Raids(r).Bosses(b).Boss)
                        If SC.Raids(r).Bosses(b).Characters IsNot Nothing Then
                            For c = 0 To SC.Raids(r).Bosses(b).Characters.Length - 1
                                AddList(textClass, SC.Raids(r).Bosses(b).Characters(c).CharaClass)
                                AddList(textChara, SC.Raids(r).Bosses(b).Characters(c).CharaName)
                            Next
                        End If
                    Next
                End If
            Next
        End If

        If textDataUrl.Items.Count > 0 Then
            textDataUrl.SelectedIndex = 0
        End If

        textDataUrl2.SelectedIndex = 0
        textArea.SelectedIndex = 0
        textBoss.SelectedIndex = 0
        textClass.SelectedIndex = 0
        textChara.SelectedIndex = 0
    End Sub

    Private Sub AddList(ByRef listBox As ComboBox, ByVal text As String)
        Dim i As Long

        If listBox.Items.Count = 0 Then
            Try
                listBox.Items.Add(text)
            Catch ex As Exception
            End Try
        Else
            For i = 0 To listBox.Items.Count - 1
                If listBox.Items(i) = text Then
                    Exit Sub
                End If
            Next
            Try
                listBox.Items.Add(text)
            Catch ex As Exception
            End Try
        End If
    End Sub

    Public Class ProgNotify
        Inherits WowCrawler.DataAccessProgressNotify

        Public NotifyText As Object

        Public Overrides Sub Notify(ByVal Message As String)
            Try
                formMain.ToolStripStatusLabel1.Text = Message
                My.Application.DoEvents()
            Catch ex As Exception

            End Try
        End Sub
    End Class

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click

    End Sub
End Class

